import { ColumnConfig } from "./ColumnConfig"
import { ColumnButton } from "./ColumnButton";
export class MoreButton {
  constructor(h, params, actions) {
    this._vue = ColumnConfig.Vue;
    let moreArray = [];
    let buffer = {};
    actions.forEach(action => {
      const { power } = action;
      if (!power) { return; }
      buffer[power.code] = action.click;
      // before 属性用于验证记录是否可以显示，参数为行数据，返回boolean
      if (typeof action.before === 'function' && !action.before(params.row)) {
        return;
      }
      moreArray.push(
        h("DropdownItem", { props: { name: power.code } }, power.text)
      );
    });
    if (moreArray.length) {
      const on = {
        "on-click": name => {
          let click = buffer[name];
          if (typeof click === "function") {
            click(params.row);
          } else {
            this._vue.$Notice.warning({
              title: "系统故障",
              desc: `未设置按钮处理程序（点击事件）`
            });
          }
        }
      }
      const moreButton = new ColumnButton(h, "更多", "ios-more").build()
      const dropdownMenu = h("DropdownMenu", { slot: "list" }, moreArray);
      return h("Dropdown", { on }, [moreButton, dropdownMenu]);
    }
  }
}
